package net.wanho.systemSettings.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.customerManage.po.LinkMan;
import net.wanho.customerManage.query.LinkManQuery;
import net.wanho.systemSettings.po.Position;
import net.wanho.systemSettings.query.PositionQuery;
import net.wanho.systemSettings.service.PositionService;
import org.apache.commons.beanutils.BeanUtils;

@WebServlet("/EmmPositionServlet")
public class EmmPositionServlet extends HttpServlet {
	private PositionService positionService = BeanFactory.getBean(PositionService.class) ;
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String methodName = request.getParameter("method");
		try {
			Method method = getClass().getDeclaredMethod(methodName,
					HttpServletRequest.class, HttpServletResponse.class);
			method.setAccessible(true);
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	protected void getAllEmmPosition(HttpServletRequest request,HttpServletResponse response) {
		try {
			PositionQuery positionQuery = new PositionQuery();
			BeanUtils.populate(positionQuery,request.getParameterMap());
			//调用业务处理层
			List<Position> positionList = positionService.selectByCondPosition(positionQuery);
			//放在作用域
			request.setAttribute("positionList",positionList);
			request.getRequestDispatcher("/WEB-INF/view/systemSettings/position/index.jsp").forward(request, response);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}

	protected void addPosition(HttpServletRequest request,HttpServletResponse response) throws IOException {
		try {
			request.getRequestDispatcher("/WEB-INF/view/systemSettings/position/create.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	protected void positionNameCheck(HttpServletRequest request,HttpServletResponse response){
		JSONObject json = new JSONObject();
		try {
			response.getWriter().write(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	protected void deletePostionCheck(HttpServletRequest request,HttpServletResponse response) throws IOException{
		JSONObject json = new JSONObject();
		response.getWriter().write(json.toString());
	}
	
	
	protected void deletePosition(HttpServletRequest request,HttpServletResponse response){
		try {
			response.sendRedirect(request.getContextPath()+ "/EmmPositionServlet?method=getAllEmmPosition");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	protected void getUpdatePostionDetailById(HttpServletRequest request,HttpServletResponse response){
		try {
			String positionId = request.getParameter("positionId");
			Position position = positionService.selectByIdPosition(Integer.parseInt(positionId));
			request.setAttribute("position",position);
			request.getRequestDispatcher("/WEB-INF/view/systemSettings/position/update.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}
	
	protected void updatePosition(HttpServletRequest request,HttpServletResponse response){
	
		try {	
			response.sendRedirect(request.getContextPath()+ "/EmmPositionServlet?method=getAllEmmPosition");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	protected void getLinkPostionDetailById(HttpServletRequest request,HttpServletResponse response){
		try {
			request.getRequestDispatcher("/WEB-INF/view/systemSettings/position/references.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	protected void positionLinks(HttpServletRequest request,HttpServletResponse response){
		try {
			response.sendRedirect(request.getContextPath()+ "/EmmPositionServlet?method=getAllEmmPosition");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
